<?php
if (!defined('IN_ELS'))
{
    die('Hacking attempt');
}
class control extends membersbase{
	
	/*
	'==========================================================================================
	'〖名 称〗:control构造
	'〖参 数〗:
	'==========================================================================================
	*/
	function control() {
		$this->membersbase();
		$act=empty($_REQUEST["a"]) ? "empty" : $_REQUEST["a"];
		if($act=="empty")				$this->index();//用户首页
		if($act=="upload_img")			$this->upload_img();//用户上传图片
		if($act=="logout")				$this->logout();//退出
	}
	
	//用户首页
	function index()
	{
		if($_SESSION['mid']){
			$_SESSION['up_img_show'] = session_id();
			
			$m_info = $this->getUserInfo($_SESSION['mid']);
			$this->view->assign('m_info'  ,$m_info);

			$footer_list = ($this->get_footer());
			$this->view->assign("footer_list",$footer_list);// 底部连接 footer


			$this->view->display("members");
		}else{
			$this->pageRedirect("index.php");
		}
	}
	//用户首页
	function logout()
	{
		$_SESSION['mid'] = '';
		$this->pageRedirect("index.php");
	}
	
	//用户上传图片
	function upload_img()
	{
		if($_SESSION['mid']){
			
			$name1 = gmdate('Ym');
			//创建用户文件夹
			$file_dir = 'zs_img/'.$_SESSION['mid'];
			if(!@file_exists($file_dir)){ 
				@mkdir($file_dir,   0777);   
			}
			$file_dir_big = 'zs_img/'.$_SESSION['mid'].'/big';
			if(!@file_exists($file_dir_big)){ 
				@mkdir($file_dir_big,   0777);   
			}
			$file_dir_s = 'zs_img/'.$_SESSION['mid'].'/small';
			if(!@file_exists($file_dir_s)){ 
				@mkdir($file_dir_s,   0777);   
			}
			$file_dirss_big = 'zs_img/'.$_SESSION['mid'].'/big/'.$name1;
			if(!@file_exists($file_dirss_big)){ 
				@mkdir($file_dirss_big,   0777);   
			}
			$file_dirss_small = 'zs_img/'.$_SESSION['mid'].'/small/'.$name1;
			if(!@file_exists($file_dirss_small)){ 
				@mkdir($file_dirss_small,   0777);   
			}
			$save_path_b = $file_dirss_big . "/";//保存路径
			$save_path_s = $file_dirss_small . "/";//保存路径
			$upload_name = "Filedata";
			$max_file_size = 5000*5000;				// 1MB in bytes
			$ext = array( "jpg", "gif", "png");	//允许的文件名称
			$extend = explode ( ".", $_FILES[$upload_name]['name'] );
			$extend = array_pop($extend);
			if (!isset($_FILES[$upload_name])) {
				$this->HandleError("没有找到要上传的文件 " . $upload_name);
				exit(0);
			} else if (isset($_FILES[$upload_name]["error"]) && $_FILES[$upload_name]["error"] != 0) {
				$this->HandleError($uploadErrors[$_FILES[$upload_name]["error"]]);
				exit(0);
			} else if (!isset($_FILES[$upload_name]["tmp_name"]) || !@is_uploaded_file($_FILES[$upload_name]["tmp_name"])) {
				$this->HandleError("上传失败");
				exit(0);
			} else if (!isset($_FILES[$upload_name]['name'])) {
				$this->HandleError("文件没有名称.");
				exit(0);
			}
		         if( in_array($extend,$ext) == false ){
		           $this-> HandleError("不允许的文件扩展名 " . $upload_name);
				exit(0);
		        }
			$file_size = @filesize($_FILES[$upload_name]["tmp_name"]);
			if (!$file_size || $file_size > $max_file_size) {
				$this->HandleError("上传文件超出允许的大小");
				exit(0);
			}
       		$string=$this->rand_strings(6);//随机字符串
	        $file_name=time().'_'.$string.'.'.$extend;//图片名称
			if (file_exists($save_path_b . $file_name)) {
				$this->HandleError("文件名已存在");
				exit(0);
			}
			
			if (!@move_uploaded_file($_FILES[$upload_name]["tmp_name"], $save_path_b.$file_name)) {
				$this->HandleError("文件无法保存.");
				exit(0);
			}else{
			
				
				$picture_product_path = $save_path_b.$file_name;
				$picture_product_paths = $save_path_s.$file_name;
				$l1=strrpos($picture_product_path,".");
				$typeImg=substr($picture_product_paths,$l1+1);
				$path=str_replace(".".$typeImg,"",$picture_product_paths);
				
				$im=imagecreatefromjpeg($picture_product_path); 
				
				$this->resizeImage($im,300,590,$path.".min",".".$typeImg);
				
				$picture_min=$path.".min.".$typeImg;
				
				//添加入库
				$dataArray['user_id']			= $_SESSION[mid];
				$dataArray['image_url']			= $picture_product_path;
				$dataArray['image_url_s']		= $picture_min;
				$dataArray['is_ok']				= 1;
				$dataArray['biaoshi']			= session_id();
				$dataArray['create_time']		= $this -> getCurrTimeLine();
				$retValue = $this->insertimg($dataArray);
				
			}
		 	echo "File Received";
			exit(0);
		}else{
			$this->pageRedirect("index.php");
		}
	}
	function HandleError($message) {
		echo $message;
	}
	function rand_strings($length){
	    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	    for($i=0;$i<$length;$i++){
	        $string.=substr($chars, rand(0,strlen($chars)-1),1);
	    }
	    return $string;
	}
	/**
	 * 获取文件后缀名
	 *
	 * @param string $file_name
	 * @return string
	 */
	function getFileExt($file_name) {
		$extend = explode ( ".", $file_name );
		$va = array_pop($extend);
		return strtolower($va);
	}
}
?>